/**
 *
 * by littlefean
 */

window.onload = function () {
    document.querySelector(`.renew`).addEventListener("click", () => {
        window.location.reload();
    });
    let inputMatrix = document.querySelector(`.inputMatrix`);
    let maxN = 5;

    let size = {
        width: 0,
        height: 0,
    }

    /**
     * 获取用户输入的矩阵
     * @return {[]} 二维数组表示矩阵
     */
    function getMInput() {
        let oriM = [];
        for (let y = 0; y < size.height; y++) {
            let line = [];
            for (let x = 0; x < size.width; x++) {
                line.push(+inputMatrix.children[y].children[x].value);
            }
            oriM.push(line);
        }
        return oriM;
    }

    /**
     * 把一个矩阵写到html元素里去
     * @param arr2d
     * @param ele
     */
    function writeM(arr2d, ele) {
        for (let y = 0; y < arr2d.length; y++) {
            let lineEle = div("", "line");
            for (let x = 0; x < arr2d[0].length; x++) {
                let boxEle = div(arr2d[y][x].toString(), "box");
                lineEle.appendChild(boxEle);
            }
            ele.appendChild(lineEle);
        }
    }

    for (let y = 0; y < maxN; y++) {
        let lineEle = div("", "line");

        for (let x = 0; x < maxN; x++) {
            let box = div(`${y + 1}x${x + 1}`, "box");

            box.addEventListener("click", () => {
                size.width = x + 1;
                size.height = y + 1;
                // 清除大小选择
                inputMatrix.innerHTML = "";
                document.querySelector(`.note`).innerHTML = "现在开始输入你的矩阵里的每一个数字吧";
                // 开始进行输入矩阵
                for (let i = 0; i < size.height; i++) {
                    let line = div("", "line");
                    for (let j = 0; j < size.width; j++) {
                        let box = document.createElement("input");
                        box.classList.add("box");
                        box.placeholder = "0";
                        box.type = "Number";
                        box.onblur = () => {
                            console.log(123);
                            getT();
                            getStar();
                            getRe();
                        };
                        line.appendChild(box);
                    }
                    inputMatrix.appendChild(line);
                }
            });
            lineEle.appendChild(box);
        }
        inputMatrix.appendChild(lineEle);
    }
    // 求转置
    let out1 = document.querySelector(`.out1`);

    /**
     * 直接获取输入矩阵里的元素内容，更新out1
     */
    function getT() {
        if (size.width === 0 || size.height === 0) {
            out1.innerHTML = "<p class='note'>还没有输入矩阵哦！</p>"
        } else {
            out1.innerHTML = "";
            let oriM = getMInput();
            let sM = matrixT(oriM);
            writeM(sM, out1);
        }
    }

    // 伴随
    let out2 = document.querySelector(`.out2`);

    function getStar() {
        if (size.width === 0 || size.height === 0) {
            out2.innerHTML = "<p class='note'>还没有输入矩阵哦！</p>"
        } else if (size.width !== size.height) {
            out2.innerHTML = "<p class='note'>非方阵不能求伴随哦!</p>"
        } else {
            out2.innerHTML = "";
            let oriM = getMInput();
            let sM = matrixS(oriM);
            writeM(sM, out2);
        }
    }

    // 逆矩阵
    let out3 = document.querySelector(`.out3`);

    function getRe() {
        if (size.width === 0 || size.height === 0) {
            out3.innerHTML = "<p class='note'>还没有输入矩阵哦！</p>"
        } else if (size.width !== size.height) {
            out3.innerHTML = "<p class='note'>非方阵不能求逆矩阵哦</p>"
        } else {
            out3.innerHTML = "";
            let oriM = getMInput();
            writeM(matrixR(oriM), out3);
        }
    }
}
